<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            抢单列表
        </div>
        <div class="unline underm"></div>
        <estate-search :searchConfig="searchConfig" @searchParams="search"></estate-search>
        <div class="admin_table_handle_btn">
            <!--            <a-badge style="margin-right:20px">-->
            <!--                <a-button @click="settlement()">批量结算</a-button>-->
            <!--            </a-badge>-->

            <!--            <a-badge style="margin-right:20px">-->
            <!--                <a-button @click="delivery()">批量完成配送</a-button>-->
            <!--            </a-badge>-->

        </div>
        <div class="admin_table_list">
            <a-table :row-selection="rowSelection"
                     :columns="columns" :data-source="list" :pagination="false"
                     row-key="id">

                <span slot="name" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="img">
                            <img v-if="rows.order.order_image" :src="rows.order.order_image">
                            <a-icon v-else type="picture"/></div>
                        <div class="text">{{ rows.order.order_name }}</div>
                        <div class="clear"></div>
                    </div>
                </span>

                <span slot="order_no" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="text">{{ rows.order.order_no }}</div>
                        <div class="clear"></div>
                    </div>
                </span>

                <span slot="store_name" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="text">{{ rows.order.store.store_name }}</div>
                        <div class="clear"></div>
                    </div>
                </span>
                <span slot="buyer_name" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="text">{{ rows.order.user.username }}</div>
                        <div class="clear"></div>
                    </div>
                </span>

                <span slot="buyer_name" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="text">{{ rows.order.user.username }}</div>
                        <div class="clear"></div>
                    </div>
                </span>


                <span slot="created_at" slot-scope="rows">
                    <div class="admin_pic_txt">
                        <div class="text">{{ rows.order.created_at }}</div>
                        <div class="clear"></div>
                    </div>
                </span>

                <span slot="order_status" slot-scope="rows">
                    <a-tag color="red" v-if="rows.stop==0">已失效 </a-tag>
                    <a-tag color="orange" v-if="rows.stop==1">配送中</a-tag>
                    <a-tag color="green" v-if="rows.stop==2">已完成</a-tag>
                      <a-tag color="yellow" v-if="rows.stop==3">已结算</a-tag>
                </span>
                <span slot="action" slot-scope="rows">
                    <a-button icon="read" @click="$router.push('/Estate/orders/form/'+rows.order.id)">查看订单</a-button>
                    <!--                    <a-button icon="edit" @click="$router.push('/Estate/orders/form/'+rows.order.id)">完成配送</a-button>-->
                </span>
            </a-table>
            <div class="admin_pagination" v-if="total>0">
                <a-pagination v-model="params.page" :page-size.sync="params.per_page"
                              :total="total" @change="onChange"
                              show-less-items/>
            </div>
        </div>
    </div>
</template>

<script>

import estateSearch from '@/components/estate/search'

export default {
    components: {estateSearch},
    props: {},
    data() {
        return {
            params: {
                page: 1,
                per_page: 30,
            },
            searchConfig: [
                {label: '用戶', name: 'user_id', type: 'select', data:[]},
                {label: '抢单时间', name: 'created_at', type: 'date_picker'},
                // {label: '店铺', name: 'store_id', type: 'select', data:[]},
            ],
            total: 0, //总页数
            selectedRowKeys: [], // 被选择的行
            columns: [
                //   {title:'#',dataIndex:'id',fixed:'left'},
                {title: '订单名称', key: 'id', fixed: 'left', scopedSlots: {customRender: 'name'}},
                {title: '订单号', key: 'id', fixed: 'left', scopedSlots: {customRender: 'order_no'}},
                {title: '店铺', key: 'id', fixed: 'left', scopedSlots: {customRender: 'store_name'}},
                {title: '买家', key: 'id', fixed: 'left', scopedSlots: {customRender: 'buyer_name'}},
                {title: '状态', key: 'id', fixed: 'left', scopedSlots: {customRender: 'order_status'}},
                {title: '下单时间', key: 'id', fixed: 'left', scopedSlots: {customRender: 'created_at'}},
                {title: '操作', key: 'id', fixed: 'right', scopedSlots: {customRender: 'action'}},
            ],
            list: [],
        };
    },

    watch: {},
    computed: {

        rowSelection() {
            return {
                onChange: (selectedRowKeys, selectedRows) => {
                    this.selectedRowKeys = selectedRowKeys;
                    // console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
                },
                getCheckboxProps: record => ({
                    props: {
                        disabled: record.stop == 3, // Column configuration not to be checked
                    },
                }),
            };
        },
    },
    methods: {
        search(params) {
            let page = this.params.page;
            let per_page = this.params.per_page;

            // 事件需要格式化，后面再看看有没有更好得到办法
            if (!this.$isEmpty(params.created_at) && !this.$isEmpty(params.created_at[0])) {
                params.created_at[0] = moment(params.created_at[0]).format('YYYY-MM-DD HH:mm:ss');
                params.created_at[1] = moment(params.created_at[1]).format('YYYY-MM-DD HH:mm:ss');
            }
            this.params = params;
            this.params.page = page;
            this.params.per_page = per_page;
            this.onload();
        },
        // 选择框被点击
        onSelectChange(selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys;
        },
        // 选择分页
        onChange(e) {
            this.params.page = e;
        },
        // 删除
        delivery() {
            if (this.selectedRowKeys.length == 0) {
                return this.$message.error('未选择数据.');
            }
            this.$confirm({
                title: '确定要完成？',
                content: '确定结完成',
                okText: '是',
                okType: 'danger',
                cancelText: '取消',
                onOk: () => {
                    let ids = this.selectedRowKeys.join(',');
                    this.$post(this.$api.grabOrderChange, {ids: ids, stop: 2}).then(res => {
                        if (res.code == 200) {
                            this.onload();
                            this.$message.success('成功');
                        } else {
                            this.$message.error(res.msg)
                        }
                    });

                },
            });
        },

        //批量结算
        settlement() {

            if (this.selectedRowKeys.length == 0) {
                return this.$message.error('未选择数据.');
            }
            this.$confirm({
                title: '确定要结算？',
                content: '确定结算后无法恢复.',
                okText: '是',
                okType: 'danger',
                cancelText: '取消',
                onOk: () => {
                    let ids = this.selectedRowKeys.join(',');
                    this.$post(this.$api.grabOrderChange, {ids: ids, stop: 3}).then(res => {
                        if (res.code == 200) {
                            this.onload();
                            this.$message.success('结算成功');
                        } else {
                            this.$message.error(res.msg)
                        }
                    });

                },
            });
        },
        onload() {
            if (!this.$isEmpty(this.$route.query.is_refund)) {
                this.params.is_refund = 1;
            }
            if (!this.$isEmpty(this.$route.query.is_return)) {
                this.params.is_return = 1;
            }
            this.$post(this.$api.grabList, this.params).then(res => {
                this.total = res.data.list.total;
                this.list = res.data.list.data;
                this.searchConfig[0].data = res.data.user_list;
                this.searchConfig[2].data = res.data.store_list;
            });

        },
    },
    created() {
        this.onload();

    },
    mounted() {
    }
};
</script>
<style lang="scss" scoped>

</style>
